home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / aros / dos / fputc.c < prev    next >
C/C++ Source or Header  |  1996-09-12  |  3KB  |  137 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: fputc.c,v 1.4 1996/08/13 13:52:46 digulla Exp $
  4.     $Log: fputc.c,v $
  5.     Revision 1.4  1996/08/13 13:52:46  digulla
  6.     Replaced <dos/dosextens.h> by "dos_intern.h" or added "dos_intern.h"
  7.     Replaced __AROS_LA by __AROS_LHA
  8.  
  9.     Revision 1.3  1996/08/12 14:22:57  digulla
  10.     Removed irritating empty line
  11.  
  12.     Revision 1.2  1996/08/01 17:40:51  digulla
  13.     Added standard header for all files
  14.  
  15.     Desc:
  16.     Lang: english
  17. */
  18. #include <exec/memory.h>
  19. #include <clib/exec_protos.h>
  20. #include <dos/dosextens.h>
  21. #include "dos_intern.h"
  22.  
  23. /*****************************************************************************
  24.  
  25.     NAME */
  26.     #include <clib/dos_protos.h>
  27.  
  28.     __AROS_LH2(LONG, FPutC,
  29.  
  30. /*  SYNOPSIS */
  31.     __AROS_LHA(BPTR, file,      D1),
  32.     __AROS_LHA(LONG, character, D2),
  33.  
  34. /*  LOCATION */
  35.     struct DosLibrary *, DOSBase, 52, Dos)
  36.  
  37. /*  FUNCTION
  38.  
  39.     INPUTS
  40.     file      - Filehandle to write to.
  41.     character - Character to write.
  42.  
  43.     RESULT
  44.     The character written or EOF in case of an error.
  45.     IoErr() gives additional information in that case.
  46.  
  47.     NOTES
  48.  
  49.     EXAMPLE
  50.  
  51.     BUGS
  52.  
  53.     SEE ALSO
  54.     FGetC(), IoErr()
  55.  
  56.     INTERNALS
  57.  
  58.     HISTORY
  59.     29-10-95    digulla automatically created from
  60.                 dos_lib.fd and clib/dos_protos.h
  61.  
  62. *****************************************************************************/
  63. {
  64.     __AROS_FUNC_INIT
  65.     __AROS_BASE_EXT_DECL(struct DosLibrary *,DOSBase)
  66.  
  67.     /* Get pointer to result. */
  68.     LONG *result=&((struct Process *)FindTask(NULL))->pr_Result2;
  69.  
  70.     /* Get pointer to filehandle */
  71.     struct FileHandle *fh=(struct FileHandle *)BADDR(file);
  72.  
  73.     /* Check if file is in write mode */
  74.     if(!(fh->fh_Flags&FHF_WRITE))
  75.     {
  76.     if(fh->fh_Pos<fh->fh_End)
  77.         /* Read mode. Try to seek back to the current position. */
  78.         if(Seek(file,fh->fh_Pos-fh->fh_End,OFFSET_CURRENT)<0)
  79.         {
  80.         fh->fh_Pos=fh->fh_End=fh->fh_Buf;
  81.         return EOF;
  82.         }
  83.  
  84.     /* Is there a buffer? */
  85.     if(fh->fh_Buf==NULL)
  86.     {
  87.         /* No. Get one. */
  88.         fh->fh_Buf=AllocMem(IOBUFSIZE,MEMF_ANY);
  89.         if(fh->fh_Buf==NULL)
  90.         {
  91.         /* Couldn't get buffer. Return error. */
  92.         *result=ERROR_NO_FREE_STORE;
  93.         return EOF;
  94.         }
  95.         /* Got it. Use it. */
  96.         fh->fh_Flags|=FHF_BUF;
  97.         fh->fh_Size=IOBUFSIZE;
  98.     }
  99.  
  100.     /* Prepare buffer */
  101.     fh->fh_Pos=fh->fh_Buf;
  102.     fh->fh_End=fh->fh_Buf+fh->fh_Size;
  103.     fh->fh_Flags|=FHF_WRITE;
  104.     }
  105.  
  106.     /* Check if there is still some space in the buffer */
  107.     if(fh->fh_Pos>=fh->fh_End)
  108.     {
  109.     UBYTE *pos;
  110.  
  111.     /* Write the data. (In many pieces if the first one isn't enough). */
  112.     pos=fh->fh_Buf;
  113.     while(pos!=fh->fh_Pos)
  114.     {
  115.         LONG size;
  116.         size=Write(file,pos,fh->fh_Pos-pos);
  117.  
  118.         /* An error happened? No success. */
  119.         if(size<0)
  120.         {
  121.         fh->fh_Pos=fh->fh_End=fh->fh_Buf;
  122.         fh->fh_Flags&=~FHF_WRITE;
  123.         return EOF;
  124.         }
  125.         pos+=size;
  126.     }
  127.  
  128.     /* Reset buffer */
  129.     fh->fh_Pos=fh->fh_Buf;
  130.     }
  131.  
  132.     /* Write data and return */
  133.     *fh->fh_Pos++=character;
  134.     return character;
  135.     __AROS_FUNC_EXIT
  136. } /* FPutC */
  137.